//构建索引模块
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include"util.hpp"
#include<unordered_map>
#include<mutex>
namespace ns_index
{
    struct DocInfo
    {
        std::string title;
        std::string content;
        std::string url;
        uint64_t doc_id; 
    };
    //查找到的一个元素需要有什么
    struct InvertedElem
    {
        uint64_t doc_id;
        std::string word;
        int weight;
        InvertedElem():weight(0)
        {

        }
    };
    
    

    class Index
    {
    private:
        Index()
        {

        }
        Index(const Index&)  = delete;
        Index& operator=(const Index&) = delete;
        static Index* instance;
        static std::mutex mtx;
    public:
        static Index* GetInstance()
        {
            if(nullptr == instance)
            {
                mtx.lock();
                if(nullptr == instance)
                {
                    instance = new Index();
                }
                mtx.unlock();
            }
            return instance;
        }
    public:
        std::vector<InvertedElem> * GetInvertedList(const std::string& word)
        {
            //按词找到所有的倒排拉链
            auto iter = Inverted_list.find(word);
            if(iter == Inverted_list.end())
            {
                std::cerr<<"have no InverteredList!"<<std::endl;
                return nullptr;
            }
            return &(iter->second);
        }
        
        DocInfo* GetForwardIndex(uint64_t doc_id)
        {
            if(doc_id >= forward_list.size())
            {
                std::cerr<<"doc_id out range!"<<std::endl;
                return nullptr;
            }
            return &forward_list[doc_id];
        }
        bool BuildIndex(const std::string input)
        {
            //首先需要将文本的内容读取出来
            std::ifstream in(input,std::ios::in|std::ios::binary);
            if(!in.is_open())
            {
                std::cerr<<"open "<<input << " error !" <<std::endl;
                return false;
            }
            std::string line;
            while(std::getline(in,line))
            {
                //一行就是一个文件的内容
                DocInfo * doc = BuildForwardIndex(line);
                if(nullptr == doc)
                {
                    std::cerr<<"Build:" << line<<" error !"<<std::endl;
                    continue;
                }
                BuildInvertedIndex(*doc);
            }
            in.close();
            return true;
        }
    private:
        DocInfo* BuildForwardIndex(const std::string& line)
        {
            const std::string SEP = "\3";
            //将文本进行解析，通过库来实现
            std::vector<std::string>results;
            ns_util::StringSplit::SplitString(line,&results,SEP);
            if(results.size() != 3)
            {
                return nullptr;
            }
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_list.size();
            forward_list.push_back(doc);
            return &(forward_list.back());
        }

        bool BuildInvertedIndex(const DocInfo& doc)
        {
            //统计词频
            struct word_cnt
            {
                int title_cnt;
                int content_cnt;
                word_cnt():title_cnt(0),content_cnt(0){}
            };
            std::unordered_map <std::string,word_cnt> word_map;
            //对标题进行分词
            std::vector<std::string>title_words;
            ns_util::JiebaUtil::CutString(doc.title,&title_words);
            //进行词频统计
            for(auto s : title_words)
            {
                boost::to_lower(s);
                word_map[s].title_cnt++;
            }
            //对内容进行分词
            std::vector<std::string>content_words;
            ns_util::JiebaUtil::CutString(doc.content,&content_words);
            for(auto s : content_words)
            {
                boost::to_lower(s);
                word_map[s].content_cnt++;
            }
            //现在已经保存了所有的词频率
            #define X 10
            #define Y 1
            //遍历每一个词
            for(auto word_pair : word_map)
            {
                InvertedElem item;
                item.doc_id = doc.doc_id;
                item.word = word_pair.first;
                item.weight = X * word_pair.second.title_cnt + Y * word_pair.second.content_cnt;
                std::vector<InvertedElem> & inverted_list = Inverted_list[word_pair.first];
                inverted_list.push_back(item);
            }
            return true;
        }
    private:
        //正排索引
        std::vector<DocInfo> forward_list;
        std::unordered_map< std::string,std::vector<InvertedElem> >Inverted_list;
    };
    Index* Index::instance = nullptr;
    std::mutex Index::mtx;
}